package com.base.framework.web.action;

import java.util.List;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.ConcurrentMap;

import javax.servlet.http.HttpSession;

import org.apache.commons.lang3.StringUtils;

//import com.extc.demo.utils.constant.UserConstants;
import com.jfinal.core.Controller;
import com.jfinal.kit.JsonKit;

/**
 * 
 * BaseController 控制器基类，继承于Controller TODO 提供通用的方法操作
 * 
 * @see Controller
 * @author 罗盛力 2013-9-10 下午10:47:20
 */
public class BaseController extends Controller {
	protected boolean flag = false;
	protected String msg = "";
	protected static final String SUCCESS = "success";
	protected static final String FAIL = "fail";
	protected static final String EXECUTE_RESULT = "result";
	protected static final String TOTLA_COUNT = "totalCount";
	protected static final String EXECUTE_MSG = "msg";
	protected ConcurrentMap<String, Object> responseMap;

//	/**
//	 * 获得登录用户的ID
//	 * 
//	 * @return
//	 */
//	protected int getUserId() {
//		HttpSession session = getSession();
//		Object sessionObj = session.getAttribute(UserConstants.LOGIN_SUBJECT);
//		if (sessionObj != null) {
//			@SuppressWarnings("unchecked")
//			ConcurrentMap<String, Object> subjectMap = (ConcurrentMap<String, Object>) sessionObj;
//			return (Integer) subjectMap.get(UserConstants.LOGIN_SUBJECT_ID);
//		}
//		throw new NullPointerException("未找到登录的Id");
//
//	}

	/**
	 * 返回结果
	 * 
	 * @param executeResult
	 *            执行结果判断
	 * @param msgTip
	 *            提示信息
	 */
	protected void renderJson(boolean executeResult, String msgTip) {
		responseMap = new ConcurrentHashMap<String, Object>();
		responseMap.put(SUCCESS, true);
		responseMap.put(EXECUTE_RESULT, executeResult);
		responseMap.put(EXECUTE_MSG, msgTip);
		renderJson(responseMap);
	}

	/**
	 * 返回数据集合（不含总条数）
	 * 
	 * @param executeResult
	 *            执行结果判断
	 * @param msgTip
	 *            提示信息
	 */
	@SuppressWarnings("rawtypes")
	protected void renderJson(String rootName, List resultList) {
		responseMap = new ConcurrentHashMap<String, Object>();
		responseMap.put(SUCCESS, true);
		responseMap.put(rootName, resultList);
		renderJson(responseMap);
	}

	/**
	 * 返回数据集合（含总条数）
	 * 
	 * @param executeResult
	 *            执行结果判断
	 * @param msgTip
	 *            提示信息
	 */
	protected void renderJson(String rootName, List<Object> resultList,
			long totalCount) {
		responseMap = new ConcurrentHashMap<String, Object>();
		responseMap.put(SUCCESS, true);
		responseMap.put(rootName, resultList);
		responseMap.put("totalCount", totalCount);
		renderJson(responseMap);
	}

	/**
	 * 处理JSONP返回
	 * 
	 * @param obj
	 */
	protected void renderJsonP(Object obj) {
		String callback = getPara("callback");
		String jsonStr = JsonKit.toJson(obj);
		String jsonString = "{success: true}";
		String responseString = "";
		if (isNotBlank(callback)) {
			responseString = callback + "(" + jsonStr + ");";
		} else {
			responseString = jsonString;
		}
		renderJson(responseString);
	}

	/**
	 * 检测是否为空字符串
	 * 
	 * @param str
	 *            需判断的字符串
	 * @return flag==true 为非空
	 */
	protected boolean isNotBlank(String str) {
		if (StringUtils.isNotBlank(str)) {
			flag = true;
		} else {
			flag = false;
		}
		return flag;
	}

	/**
	 * 判断对象是否非空
	 * 
	 * @param obj
	 *            需判断的对象
	 * @return flag==true 为非空
	 */
	protected boolean isNotNullObj(Object obj) {
		if (obj != null) {
			flag = true;
		} else {
			flag = false;
		}
		return flag;
	}

}
